<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="email"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="pwd"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit"> 提交 </van-button>
      </div>
    </van-form>
  </div>
</template>

<script lang="ts" setup>
import { useUserHooks } from '@/stores/user'
import { RSA } from '@/utils'
import { request } from '@/utils/request'
import { showNotify } from 'vant'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const username = ref('906446244@qq.com')
const password = ref('123456')

const onSubmit = async (values: any) => {
  // 加密密码
  values.pwd = RSA(values.pwd)

  const resp = await request({
    url: '/health/doctor/v1/login',
    method: 'post',
    data: values,
    // 格式 form 格式
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
  const { status, message, result } = resp.data
  if (status === '0000') {
    showNotify({ type: 'primary', message })
    // 存储数据
    useUserHooks().loginSuccess(result)
    // 进入首页
    router.push('/')
  } else {
    showNotify({ type: 'danger', message })
  }
}
</script>
